const express=require('express');
const db = require('../utils/database');
const myRouter = express.Router();
// 添加
myRouter.post('/supplier/Add',(req,res)=>{
    const sql = `INSERT INTO supplier VALUES(NULL,'${req.body.s_name}','${req.body.s_contact}','${req.body.s_tele}'
    ,'${req.body.s_address}','${req.body.s_state}')`;
// INSERT INTO supplier VALUES(NULL,'XS','ASS','13333333333','DDDDDDDD','供货中');
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            res.send({error:0,msg:'添加成功',data:data})
        }
    })
})
//获取
myRouter.get('/supplier/get',(req,res)=>{
    const page=req.query.page;
    const count=req.query.count;
    const sql=`SELECT * FROM supplier LIMIT ${(page-1)*count},${count}`;
    const sql2 = `select count(*) as total from supplier WHERE s_name LIKE '%${req.query.s_name}%' AND s_state LIKE '%${req.query.s_state}%';`;
    const sql3 = `SELECT * FROM supplier WHERE s_name LIKE '%${req.query.s_name}%' AND s_state LIKE '%${req.query.s_state}%' LIMIT ${(page-1)*count},${count}`  ;
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            db.query(sql2,(err2,data2)=>{
                if(err2){
                    console.log(err2)
                    res.send({error:1,msg:err2})
                }else{
                   db.query(sql3,(err3,data3)=>{
                if(err3){
                    console.log(err3)
                    res.send({error:1,msg:err3})
                }else{
                    // console.log(data3)
                    res.send({error:0,msg:'查询成功',data:data,total:data2[0].total,searchDate:data3})
                }
            })
                }
            })
        }
    })
})
//修改
myRouter.get('/supplier/edit', (req, res) => {

    const sql = `UPDATE supplier SET s_name='${req.query.s_name}',s_contact='${req.query.s_contact}',
    
    s_tele='${req.query.s_tele}',
    s_state='${req.query.s_state}'
    ,s_address='${req.query.s_address}' WHERE s_id='${req.query.id}';`;
  
    db.query(sql,(err,data)=>{
        if(err){
            console.log(err)
            res.send({error:1,msg:err})
        }else{
            res.send({error:0,msg:'修改成功',data:data})
        }
    })
})

module.exports=myRouter;